// miniprogram/pages/goods_detail/index.js
import {
  db
} from "../../utils/index"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    goods: {},
    likeStatus: false,
    collectStatus: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getGoodsDetail(options.goodsId);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  // 获取商品详情
  getGoodsDetail(id) {
    const goodsId = Number(id);
    console.log(goodsId, 'goodsId');
    wx.showLoading({
      mask:true
    });
    // db.collection("goods").where({
    //   goodsId:goodsId
    // }).get().then(res=>{
    //   console.log(res);
    // });
    wx.cloud.callFunction({
      name: "getGoodsById",
      data: {
        goodsId
      }
    }).then(res => {
      console.log(res, 'res');
      let goods = res.result.data;
      this.setData({
        goods
      });

      wx.cloud.callFunction({
        name: "checkLike",
        data:{goodsId: goods._id}
      }).then(res => {
        wx.hideLoading();
        this.setData({
          likeStatus: res.result.status
        });
      });
      wx.cloud.callFunction({
        name: "checkCollect",
        data:{goodsId: goods._id}
      }).then(res => {
        wx.hideLoading();
        this.setData({
          collectStatus: res.result.status
        });
      });


    });

  },
  // 提交到购物车
  handleChart(event) {
    let goods = this.data.goods;
    wx.showLoading({
      mask:true
    });
    wx.cloud.callFunction({
      name: "addOrder",
      data: {
        goods
      }
    }).then(res => {
      wx.showToast({
        title: res.result.msg,
      })
      // console.log(res,'res');
    })

  },
  pathTo() {
    wx.navigateTo({
      url: '/pages/cart_detail/index',
    })
  },
  // 添加收藏
  handleStar() {
    const collectStatus = this.data.collectStatus;
    let name = collectStatus ? "delCollect" : "addCollect";
    wx.showLoading({
      mask:true
    });
    let goodsId = this.data.goods._id;
    wx.cloud.callFunction({
      name: name,
      data: {
        id: goodsId
      }
    }).then(res => {
      if (res.result.code === 200) {
        wx.showToast({
          title: res.result.msg,
        });
        this.setData({
          collectStatus: !this.data.collectStatus
        })
      }
    }).catch(err => {})
  },

  handleBuy(){
    wx.showLoading({
      mask:true
    });
    
    let goods = this.data.goods;
    wx.cloud.callFunction({
      name: "addOrder",
      data: {
        goods
      }
    }).then(res => {
      wx.showToast({
        title: res.result.msg,
      });
      this.pathTo();
      // console.log(res,'res');
    })
  }
})